<template>
	<view class="activity-detail-page">
		<!-- 加载状态 -->
		<up-loading-icon v-if="loading" class="page-loading" />

		<!-- 活动详情 -->
		<view v-else class="activity-container">
			<!-- 活动封面 -->
			<image :src="activity.image" mode="aspectFill" class="activity-cover"></image>

			<!-- 活动信息 -->
			<view class="activity-content">
				<!-- 标题和状态 -->
				<view class="title-section">
					<text class="activity-title">{{ activity.title }}</text>
					<up-tag :text="getStatusText(activity.status)" :type="getStatusType(activity.status)" plain>
					</up-tag>
				</view>

				<!-- 活动详情 -->
				<view class="info-section">
					<view class="info-item">
						<up-icon name="clock" size="18" color="#36d399"></up-icon>
						<view class="info-content">
							<text class="info-label">活动时间</text>
							<text class="info-value">{{ activity.startTime }} 至 {{ activity.endTime }}</text>
						</view>
					</view>

					<view class="info-item">
						<up-icon name="map" size="18" color="#36d399"></up-icon>
						<view class="info-content">
							<text class="info-label">活动地点</text>
							<text class="info-value">{{ activity.location }}</text>
						</view>
					</view>

					<view class="info-item">
						<up-icon name="account" size="18" color="#36d399"></up-icon>
						<view class="info-content">
							<text class="info-label">参与人数</text>
							<text class="info-value">
								{{ activity.currentParticipants }} 人
								<text v-if="activity.maxParticipants > 0">/ {{ activity.maxParticipants }} 人</text>
							</text>
						</view>
					</view>

					<view class="info-item">
						<up-icon name="account-fill" size="18" color="#36d399"></up-icon>
						<view class="info-content">
							<text class="info-label">组织者</text>
							<text class="info-value">{{ activity.organizerName }}</text>
						</view>
					</view>
				</view>

				<!-- 活动内容 -->
				<view class="content-section">
					<text class="section-title">活动详情</text>
					<text class="activity-description">{{ activity.content }}</text>
				</view>
			</view>

			<!-- 底部报名按钮 -->
			<view class="bottom-bar">
				<up-button type="primary" @click="handleJoinActivity" :loading="joining" :disabled="isJoined"
					color="#36d399" class="join-btn">
					{{ isJoined ? '已报名' : '立即报名' }}
				</up-button>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue';
	import {
		onLoad
	} from '@dcloudio/uni-app';
	import {
		mockGetActivity
	} from '../../server/mock.js';
	import showToast from '../../utils/showToast.js';

	// 状态
	const loading = ref(true);
	const joining = ref(false);
	const isJoined = ref(false);
	const activity = ref({});
	const activityId = ref(null);

	/**
	 * 加载活动详情
	 */
	const loadActivity = async (id) => {
		loading.value = true;

		try {
			const res = await mockGetActivity(id);

			if (res.code === 200) {
				activity.value = res.data || {};
			} else {
				showToast(res.message || '加载失败', 'error');
			}
		} catch (error) {
			console.error('加载活动详情失败:', error);
			showToast('网络错误', 'error');
		} finally {
			loading.value = false;
		}
	};

	/**
	 * 获取状态文本
	 */
	const getStatusText = (status) => {
		const statusMap = {
			0: '未开始',
			1: '进行中',
			2: '已结束',
			3: '已取消'
		};
		return statusMap[status] || '未知';
	};

	/**
	 * 获取状态类型
	 */
	const getStatusType = (status) => {
		const typeMap = {
			0: 'primary',
			1: 'success',
			2: 'info',
			3: 'error'
		};
		return typeMap[status] || 'default';
	};

	/**
	 * 报名活动
	 */
	const handleJoinActivity = async () => {
		// 检查登录状态
		const token = uni.getStorageSync('token');
		if (!token) {
			showToast('请先登录', 'none');
			setTimeout(() => {
				uni.navigateTo({
					url: '/pages/login/login'
				});
			}, 1000);
			return;
		}

		// 检查活动状态
		if (activity.value.status === 2) {
			showToast('活动已结束', 'none');
			return;
		}

		if (activity.value.status === 3) {
			showToast('活动已取消', 'none');
			return;
		}

		// 检查是否已满人
		if (activity.value.maxParticipants > 0 && activity.value.currentParticipants >= activity.value
			.maxParticipants) {
			showToast('活动人数已满', 'none');
			return;
		}

		joining.value = true;

		try {
			// 模拟报名
			await new Promise(resolve => setTimeout(resolve, 500));

			isJoined.value = true;
			activity.value.currentParticipants += 1;

			showToast('报名成功', 'success');
		} catch (error) {
			console.error('报名失败:', error);
			showToast('报名失败', 'error');
		} finally {
			joining.value = false;
		}
	};

	// 页面加载
	onLoad((option) => {
		activityId.value = option.activityId;
		if (activityId.value) {
			loadActivity(activityId.value);
		} else {
			showToast('参数错误', 'error');
			setTimeout(() => {
				uni.navigateBack();
			}, 1000);
		}
	});
</script>

<style lang="scss" scoped>
	.activity-detail-page {
		min-height: 100vh;
		background-color: #f5f5f5;
		padding-bottom: 180rpx;
	}

	// 加载状态
	.page-loading {
		position: fixed;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
	}

	// 活动容器
	.activity-container {
		.activity-cover {
			width: 100%;
			height: 500rpx;
			object-fit: cover;
		}

		.activity-content {
			background-color: #ffffff;
			margin-top: -30rpx;
			border-radius: 30rpx 30rpx 0 0;
			padding: 40rpx 30rpx;

			// 标题区域
			.title-section {
				display: flex;
				align-items: flex-start;
				justify-content: space-between;
				margin-bottom: 40rpx;

				.activity-title {
					flex: 1;
					font-size: 36rpx;
					font-weight: bold;
					color: #333333;
					line-height: 1.4;
					margin-right: 20rpx;
				}
			}

			// 信息区域
			.info-section {
				margin-bottom: 40rpx;

				.info-item {
					display: flex;
					align-items: flex-start;
					margin-bottom: 30rpx;
					gap: 20rpx;

					&:last-child {
						margin-bottom: 0;
					}

					.info-content {
						flex: 1;
						display: flex;
						flex-direction: column;
						gap: 8rpx;

						.info-label {
							font-size: 24rpx;
							color: #999999;
						}

						.info-value {
							font-size: 28rpx;
							color: #333333;
						}
					}
				}
			}

			// 内容区域
			.content-section {
				padding-top: 30rpx;
				border-top: 1rpx solid #f5f5f5;

				.section-title {
					display: block;
					font-size: 30rpx;
					font-weight: bold;
					color: #333333;
					margin-bottom: 20rpx;
				}

				.activity-description {
					display: block;
					font-size: 28rpx;
					color: #666666;
					line-height: 1.8;
					white-space: pre-wrap;
				}
			}
		}

		// 底部操作栏
		.bottom-bar {
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			background-color: #ffffff;
			padding: 20rpx 30rpx;
			box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.05);
			z-index: 100;

			.join-btn {
				width: 100%;
				height: 90rpx;
				font-size: 32rpx;
				font-weight: bold;
			}
		}
	}
</style>

